﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;

namespace devtm.Templates.Helpers
{

    public class MyForEach : MyIteration
    {

        public MyForEach(CodeStatementCollection code, MyProperty source, string nameVariable)
            : base(new CodeSnippetStatement(), source.Call("MoveNext"), new CodeSnippetStatement())
        {

            // declare la variable enumerator a l'exterieur de la boucle
            var enumerator = code.Declare
            (
                "enumerator",
                source.Property("GetEnumerator", source.Type.TypeExpression.Translate(typeof(IEnumerator<>)))
            );


            // declare 'item dans la boucle for
            var v = Statements.Declare
                (
                    nameVariable,
                    source.Type.TypeExpression.SubType,
                    enumerator.Property("Current")
                );

        }


        public MyForEach(CodeStatementCollection code, MyRef source, string nameVariable) : base(new CodeSnippetStatement(), source.Call("MoveNext"), new CodeSnippetStatement())
        {

            // declare la variable enumerator a l'exterieur de la boucle
            var enumerator = code.Declare
            (
                "enumerator", 
                source.Type.TypeExpression.Translate(typeof(IEnumerator<>)), 
                source.Property("GetEnumerator")
            );


            // declare 'item dans la boucle for
            var v = Statements.Declare
                (
                    nameVariable,
                    source.Type.TypeExpression.SubType, 
                    enumerator.Property("Current")
                );
            
        }

    }

}
